package com.luck;

import com.luck.api.BlogManager;
import com.luck.api.ThinkWordsManager;
import com.luck.api.UserManager;
import com.luck.model.po.Blog;
import com.luck.model.po.ThinkWords;
import com.luck.redis.RedisConfig;
import com.luck.redis.RedisProxyService;
import com.luck.common.Constant;
import com.luck.model.pojo.PageRequest;
import com.luck.model.pojo.PageResponse;
import com.luck.model.dto.BlogInfoDto;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Hua wb on 2018/6/13.
 */
@Controller
public class HelloController {

    private static Logger logger = Logger.getLogger(HelloController.class);

    @Autowired
    private UserManager userManager;
    @Autowired
    private BlogManager blogManager;
    @Autowired
    private ThinkWordsManager thinkWordsManager;
    @Autowired
    private RedisProxyService redisProxyService;

    @RequestMapping("/")
    public ModelAndView hello() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("googlelogin");
        try {
            // 添加访问量
            redisProxyService.increase(RedisConfig.VISIT_AMOUNT, 1);
        } catch (Exception e) {
            System.out.println("redis not open...");
        }
        return modelAndView;
    }

    /**
     * 主页显示博客内容
     * @return
     */
    @RequestMapping("/home/{index}")
    public ModelAndView home(@PathVariable("index") int index) {
        ModelAndView modelAndView = new ModelAndView();
        // 博客
        PageResponse<BlogInfoDto> pageBlogInfo = blogManager.getPageBlogInfo(new PageRequest(index, 8));
        List<BlogInfoDto> list = pageBlogInfo.getList();
        modelAndView.addObject("blogs", list);
        // 直白
        List<ThinkWords> thinkWordsByNew5 = thinkWordsManager.getThinkWordsByNew5();
        modelAndView.addObject("thinkWords", thinkWordsByNew5);
        // 访问量
        String visitAmount = redisProxyService.get(RedisConfig.VISIT_AMOUNT);
        modelAndView.addObject("visitAmount", visitAmount == null ? 0 : visitAmount);
        // 总页数
        modelAndView.addObject("pageCount", pageBlogInfo.getPageCount());
        modelAndView.setViewName("home");
        return modelAndView;
    }

    /**
     * 暂时未用到
     * @return
     */
    @RequestMapping("/bottom")
    public ModelAndView bottom() {
        ModelAndView modelAndView = new ModelAndView();
        List<Blog> blogs = blogManager.getBlogs();
        modelAndView.addObject("list", blogs);
        modelAndView.setViewName("bottom");
        return modelAndView;
    }

    /**
     * 退出登录
     * @param httpSession
     * @return
     */
    @RequestMapping("/returnLogin")
    public String returnLogin(HttpSession httpSession) {
        httpSession.removeAttribute(Constant.SESSION_USER);
        httpSession.removeAttribute(Constant.SESSION_USER_ID);
        return "redirect:/";
    }

    /**
     * 用户是否登录
     * @param httpSession
     * @return
     */
    @RequestMapping("/session")
    @ResponseBody
    public String session(HttpSession httpSession) {
        // 登录信息在session 以 user 保存
        Object user = httpSession.getAttribute(Constant.SESSION_USER);
        if (user == null) {
            return null;
        }
        return user.toString();
    }

    @RequestMapping("/redis")
    @ResponseBody
    public String redisTest() {
        redisProxyService.add("redis", "hello redis ..");
        String s = redisProxyService.get("redis");
        return s;
    }

}